<?php
// $Id$

/**
 * Controller_Admin_Agent 控制器
 */
class Controller_Admin_Agent extends Controller_Abstract
{
	
    function _before_execute(){
        if($_SESSION['acct']['acclevel']<>0){
            exit('权限不足');
        }
    }

    function actionIndex()
    {
    	$append = array("acclevel"=>2);
        $this->union_table = acct::meta();
    
        $this->lists($append);
    
    }

		
	

	function actionadd()
	{    
		
	    if($this->_context->isPOST()){
	    	$this->_context->forwardUrl = url("agent/index");
	    	if($this->_context->id){
	    		$acct = acct::find(array("id"=>$this->_context->id))->getOne();
	    		$acct->password=$this->_context->password;
	    		$acct->md5pass=md5($acct->password);
				$acct->name=$this->_context->name;
				$acct->app_id=$this->_context->app_id;
				$acct->app_secret=$this->_context->app_secret;
				$acct->key=$this->_context->key;
				$acct->mch_id=$this->_context->mch_id;
				$acct->cert_pem=$this->_context->cert_pem;
				$acct->key_pem=$this->_context->key_pem;
				$acct->address=$this->_context->address;
				$acct->enable=$this->_context->isup;
				$acct->dec=$this->_context->dec;
				$acct->sh_version=$this->_context->sh_version;
				$acct->login_logo=$this->_context->login_logo;
				$acct->shipfee_open=$this->_context->shipfee_open;
				$acct->freeshipfee_orderprice=$this->_context->freeshipfee_orderprice;
				$acct->save();
				Syslog::logs ( '编辑代理信息,id :' . $acct->id );
				$this->success('设置成功');
			}else{
				$acct = new acct();
				$acct->name=$this->_context->name;
				$acct->acctname=$this->_context->acctname;
				if(!$acct->acctname){
					$this->error("用户名不能为空");
				}else{
					$user_name_check = Acctuser::checkUserName($acct->acctname);

					if($user_name_check!=0){
						$this->error("用户名不符合规则".$user_name_check);
					}
				}
	    		$acct->password=$this->_context->password;
	    		$acct->md5pass=md5($acct->password);
				$acct->app_id=$this->_context->app_id;
				$acct->app_secret=$this->_context->app_secret;
				$acct->key=$this->_context->key;
				$acct->mch_id=$this->_context->mch_id;
				$acct->cert_pem=$this->_context->cert_pem;
				$acct->key_pem=$this->_context->key_pem;
				$acct->address=$this->_context->address;
				$acct->isup=$this->_context->isup;
				$acct->dec=$this->_context->dec;
				$acct->sh_version=$this->_context->sh_version;
				$acct->login_logo=$this->_context->login_logo;
				$acct->shipfee_open=$this->_context->shipfee_open;
				$acct->freeshipfee_orderprice=$this->_context->freeshipfee_orderprice;
				$acct->save();
				Syslog::logs ( '新增代理,id :' . $acct->id );
				$this->success('设置成功');
			}

			

	        
	    }else{

	    	$acctInfo = acct::find(array("id"=>$this->_context->id))->getOne();
	    	$this->_view['info'] = $acctInfo;
	    	//var_dump($acctInfo);
	    }



		/*$id = intval($this->_context->id);
		$this->union_table = Acct::meta();

		$handle = new helper_autoinsert($this->union_table);
		$form = new Form_acct_add(url('agent/add'));


		 if ($this->_context->isPOST())
		 {
		     $adsurl=trim($this->_context->startimg);
		     global $root_dir;
		     $root_dir = str_replace ( "\\", "/", $root_dir );
		     $host = Helper_Common::getRequestHost();
		     $path = str_replace ( $host, '', $adsurl );
		     $picpath = $root_dir . '/' . 'public' . '/' . $path;
		     if(is_file($picpath)){
		         $imginfo = GetImageSize ( $picpath );
		         $imgw = $imginfo [0];
		         $imgh = $imginfo [1];
		         if($imgw<720){
		             $this->error('图片宽必须大于等于720px');
		         }
		         if($imgh<1280){
		             $this->error('图片高必须大于等于1280px');
		         }
		     }
		     
		 	if (isset($_POST['id']))
		 	{
		 		if ($_POST['id'] == $_SESSION['acct']['id'])
		 		{
		 			return $this->error('您不能修改自己');
		 		}
		 		if(!($_SESSION['acct']['admin_id'] == -1 && $_SESSION['acct']['agent_id'] == -1))
		 		{
		 			$_POST['batuserbalance'] = 1;
		 		}
		 		$_POST['acctname'] = h($_POST['acctname']);
		 	}
		 	 if (intval($this->_context->id) == 0){
		 		$isacctname = Acct::find('acctname = ?',$this->_context->acctname)->getCount('id');
		 		if ($isacctname > 0)
		 		{
		 			return $this->error('存在此帐号');
		 		}
		 	}
		 }
		
		 $result = $handle->add($form, 'id', $_GET);
		 if ($this->_context->isGET())
		 {		
		 		
		 		$this->_view['form'] = $result;
		 		$this->_viewname = '../form/add';
		 }else {
		 		Syslog::logs('修改代理:'.$_POST['acctname']);

		 		if($handle->status=='success')
		 		    $this->_context->forwardUrl = url("agent/index");
		 		return call_user_func_array(array($this,$handle->status),array($result));
		 }*/

	}
	
	
	function actiondel()
	{
	  
	    $ids = $this->_context->ids;
	    //id为2的代理不能删除
	    if(in_array(2, $ids)) $this->error('id为2的代理不能删除');
	
	
	    // Acct::rm($acct->id, $acct->admin_id);
	
	    // Syslog::logs('删除了ID:'.print_r($ids,true).'的代理');
	    // if(isset($_SESSION['_get_arrayinfo']));
	    $this->union_table = Acct::meta();
	    $this->delete(array('id'=>$this->_context->ids));
	}
	
}


